package com.linux.huhx.okhttp;

import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by huhx on 2017-07-14.
 */
public class OkHttpQrCode {
    @Test
    public void okHttpTest4() {
        String url = "http://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=mr5NUBXgsPVxwXv_qtAbMRYmKF6a5oJaeS9pq-zrQ__QnSuj3q1KfL08djW9zNiopiNnIjanoA52AF697GzswhcADsjYAwe26PxvnkfMU-Ine6Z95scQ6IfbyORQK59GQOTfAEAAYD";
        MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
        OkHttpClient client = new OkHttpClient();
        JSONObject json = new JSONObject();
        json.put("scene", "lllin/24");
        json.put("width", "430");
        RequestBody requestBody = RequestBody.create(JSON_TYPE, json.toJSONString());
        Request request = new Request.Builder().url(url).post(requestBody).build();
        try {
            Response response = client.newCall(request).execute();
            ResponseBody body = response.body();
            byte[] bytes = body.bytes();

            IOUtils.write(bytes, new FileOutputStream("D:/huhx.jpg"));

            String string = body.string();
//            System.out.println(Base64.isBase64(string)); // false
//
//            FileUtils.writeStringToFile(new File("D:/huhx.jpg"), string);
            System.out.println(string);
            body.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}